#include <string.h>
#include <stdint.h>
#include "cpu.h"

/* These are defined in link script */
extern uint8_t _sdata;
extern uint8_t _edata;
extern uint8_t _sidata;
extern uint8_t _sbss;
extern uint8_t _ebss;
extern uint8_t _estack;
extern int main(void);

/* Donit use stack and no return back */
void __attribute__((naked)) Reset_Handler(void)
{
	/* PRIMASK=1, disable all exceptions */
	CPU_DisableIrq();

	/* Init data segment */
	memcpy(&_sdata, &_sidata, &_edata-&_sdata);
	/* Init bss segment */
	memset(&_sbss, 0, &_ebss-&_sbss);

	main();
	while (1);
}

void Default_Handler(void)
{
	while(1);
}

extern void __attribute__((weak, alias("Default_Handler"))) NMI_Handler(void);
extern void __attribute__((weak, alias("Default_Handler"))) HardFault_Handler(void);
extern void __attribute__((weak, alias("Default_Handler"))) MemManage_Handler(void);
extern void __attribute__((weak, alias("Default_Handler"))) BusFault_Handler(void);
extern void __attribute__((weak, alias("Default_Handler"))) UsageFault_Handler(void);
extern void __attribute__((weak, alias("Default_Handler"))) SVC_Handler(void);
extern void __attribute__((weak, alias("Default_Handler"))) DebugMon_Handler(void);
extern void __attribute__((weak, alias("Default_Handler"))) PendSV_Handler(void);
extern void __attribute__((weak, alias("Default_Handler"))) SysTick_Handler(void);
extern void __attribute__((weak, alias("Default_Handler"))) WWDG_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) PVD_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) TAMP_STAMP_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) RTC_WKUP_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) FLASH_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) RCC_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) EXTI0_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) EXTI1_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) EXTI2_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) EXTI3_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) EXTI4_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) DMA1_Stream0_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) DMA1_Stream1_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) DMA1_Stream2_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) DMA1_Stream3_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) DMA1_Stream4_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) DMA1_Stream5_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) DMA1_Stream6_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) ADC_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) CAN1_TX_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) CAN1_RX0_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) CAN1_RX1_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) CAN1_SCE_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) EXTI9_5_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) TIM1_BRK_TIM9_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) TIM1_UP_TIM10_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) TIM1_TRG_COM_TIM11_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) TIM1_CC_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) TIM2_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) TIM3_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) TIM4_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) I2C1_EV_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) I2C1_ER_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) I2C2_EV_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) I2C2_ER_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) SPI1_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) SPI2_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) USART1_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) USART2_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) USART3_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) EXTI15_10_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) RTC_Alarm_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) OTG_FS_WKUP_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) TIM8_BRK_TIM12_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) TIM8_UP_TIM13_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) TIM8_TRG_COM_TIM14_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) TIM8_CC_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) DMA1_Stream7_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) FSMC_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) SDIO_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) TIM5_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) SPI3_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) UART4_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) UART5_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) TIM6_DAC_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) TIM7_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) DMA2_Stream0_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) DMA2_Stream1_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) DMA2_Stream2_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) DMA2_Stream3_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) DMA2_Stream4_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) ETH_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) ETH_WKUP_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) CAN2_TX_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) CAN2_RX0_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) CAN2_RX1_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) CAN2_SCE_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) OTG_FS_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) DMA2_Stream5_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) DMA2_Stream6_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) DMA2_Stream7_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) USART6_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) I2C3_EV_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) I2C3_ER_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) OTG_HS_EP1_OUT_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) OTG_HS_EP1_IN_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) OTG_HS_WKUP_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) OTG_HS_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) DCMI_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) HASH_RNG_IRQHandler(void);
extern void __attribute__((weak, alias("Default_Handler"))) FPU_IRQHandler(void);

const uint32_t __attribute__((section(".vector"))) vector_table[98] = {
	[0]  = (uint32_t)&_estack,                      /* Stack Pointer */
	[1]  = (uint32_t)Reset_Handler,                 /* Code Start */
	[2]  = (uint32_t)NMI_Handler,                   /* -14 */
	[3]  = (uint32_t)HardFault_Handler,             /* -13 */
	[4]  = (uint32_t)MemManage_Handler,             /* -12 */
	[5]  = (uint32_t)BusFault_Handler,              /* -11 */
	[6]  = (uint32_t)UsageFault_Handler,            /* -10 */
	[7]  = (uint32_t)0,
	[8]  = (uint32_t)0,
	[9]  = (uint32_t)0,
	[10] = (uint32_t)0,
	[11] = (uint32_t)SVC_Handler,                   /* -5 */
	[12] = (uint32_t)DebugMon_Handler,              /* -4 */
	[13] = (uint32_t)0,
	[14] = (uint32_t)PendSV_Handler,                /* -2 */
	[15] = (uint32_t)SysTick_Handler,               /* -1 */

	/* External Interrupts */
	[16] = (uint32_t)WWDG_IRQHandler,               /* Window WatchDog              */                                        
	[17] = (uint32_t)PVD_IRQHandler,                /* PVD through EXTI Line detection */                        
	[18] = (uint32_t)TAMP_STAMP_IRQHandler,         /* Tamper and TimeStamps through the EXTI line */            
	[19] = (uint32_t)RTC_WKUP_IRQHandler,           /* RTC Wakeup through the EXTI line */                      
	[20] = (uint32_t)FLASH_IRQHandler,              /* FLASH                        */                                          
	[21] = (uint32_t)RCC_IRQHandler,                /* RCC                          */                                            
	[22] = (uint32_t)EXTI0_IRQHandler,              /* EXTI Line0                   */                        
	[23] = (uint32_t)EXTI1_IRQHandler,              /* EXTI Line1                   */                          
	[24] = (uint32_t)EXTI2_IRQHandler,              /* EXTI Line2                   */                          
	[25] = (uint32_t)EXTI3_IRQHandler,              /* EXTI Line3                   */                          
	[26] = (uint32_t)EXTI4_IRQHandler,              /* EXTI Line4                   */                          
	[27] = (uint32_t)DMA1_Stream0_IRQHandler,       /* DMA1 Stream 0                */                  
	[28] = (uint32_t)DMA1_Stream1_IRQHandler,       /* DMA1 Stream 1                */                   
	[29] = (uint32_t)DMA1_Stream2_IRQHandler,       /* DMA1 Stream 2                */                   
	[30] = (uint32_t)DMA1_Stream3_IRQHandler,       /* DMA1 Stream 3                */                   
	[31] = (uint32_t)DMA1_Stream4_IRQHandler,       /* DMA1 Stream 4                */                   
	[32] = (uint32_t)DMA1_Stream5_IRQHandler,       /* DMA1 Stream 5                */                   
	[33] = (uint32_t)DMA1_Stream6_IRQHandler,       /* DMA1 Stream 6                */                   
	[34] = (uint32_t)ADC_IRQHandler,                /* ADC1, ADC2 and ADC3s         */                   
	[35] = (uint32_t)CAN1_TX_IRQHandler,            /* CAN1 TX                      */                         
	[36] = (uint32_t)CAN1_RX0_IRQHandler,           /* CAN1 RX0                     */                          
	[37] = (uint32_t)CAN1_RX1_IRQHandler,           /* CAN1 RX1                     */                          
	[38] = (uint32_t)CAN1_SCE_IRQHandler,           /* CAN1 SCE                     */                          
	[39] = (uint32_t)EXTI9_5_IRQHandler,            /* External Line[9:5]s          */                          
	[40] = (uint32_t)TIM1_BRK_TIM9_IRQHandler,      /* TIM1 Break and TIM9          */         
	[41] = (uint32_t)TIM1_UP_TIM10_IRQHandler,      /* TIM1 Update and TIM10        */         
	[42] = (uint32_t)TIM1_TRG_COM_TIM11_IRQHandler, /* TIM1 Trigger and Commutation and TIM11 */
	[43] = (uint32_t)TIM1_CC_IRQHandler,            /* TIM1 Capture Compare         */                          
	[44] = (uint32_t)TIM2_IRQHandler,               /* TIM2                         */                   
	[45] = (uint32_t)TIM3_IRQHandler,               /* TIM3                         */                   
	[46] = (uint32_t)TIM4_IRQHandler,               /* TIM4                         */                   
	[47] = (uint32_t)I2C1_EV_IRQHandler,            /* I2C1 Event                   */                          
	[48] = (uint32_t)I2C1_ER_IRQHandler,            /* I2C1 Error,                  */                          
	[49] = (uint32_t)I2C2_EV_IRQHandler,            /* I2C2 Event                   */                          
	[50] = (uint32_t)I2C2_ER_IRQHandler,            /* I2C2 Error,                  */                            
	[51] = (uint32_t)SPI1_IRQHandler,               /* SPI1                         */                   
	[52] = (uint32_t)SPI2_IRQHandler,               /* SPI2                         */                   
	[53] = (uint32_t)USART1_IRQHandler,             /* USART1                       */                   
	[54] = (uint32_t)USART2_IRQHandler,             /* USART2                       */                   
	[55] = (uint32_t)USART3_IRQHandler,             /* USART3                       */                   
	[56] = (uint32_t)EXTI15_10_IRQHandler,          /* External Line[15:10]s        */                          
	[57] = (uint32_t)RTC_Alarm_IRQHandler,          /* RTC Alarm (A and B) through EXTI Line */                 
	[58] = (uint32_t)OTG_FS_WKUP_IRQHandler,        /* USB OTG FS Wakeup through EXTI line */                       
	[59] = (uint32_t)TIM8_BRK_TIM12_IRQHandler,     /* TIM8 Break and TIM12         */         
	[60] = (uint32_t)TIM8_UP_TIM13_IRQHandler,      /* TIM8 Update and TIM13        */         
	[61] = (uint32_t)TIM8_TRG_COM_TIM14_IRQHandler, /* TIM8 Trigger and Commutation and TIM14 */
	[62] = (uint32_t)TIM8_CC_IRQHandler,            /* TIM8 Capture Compare         */                          
	[63] = (uint32_t)DMA1_Stream7_IRQHandler,       /* DMA1 Stream7                 */                          
	[64] = (uint32_t)FSMC_IRQHandler,               /* FSMC                         */                   
	[65] = (uint32_t)SDIO_IRQHandler,               /* SDIO                         */                   
	[66] = (uint32_t)TIM5_IRQHandler,               /* TIM5                         */                   
	[67] = (uint32_t)SPI3_IRQHandler,               /* SPI3                         */                   
	[68] = (uint32_t)UART4_IRQHandler,              /* UART4                        */                   
	[69] = (uint32_t)UART5_IRQHandler,              /* UART5                        */                   
	[70] = (uint32_t)TIM6_DAC_IRQHandler,           /* TIM6 and DAC1&2 underrun errors */                   
	[71] = (uint32_t)TIM7_IRQHandler,               /* TIM7                         */
	[72] = (uint32_t)DMA2_Stream0_IRQHandler,       /* DMA2 Stream 0                */                   
	[73] = (uint32_t)DMA2_Stream1_IRQHandler,       /* DMA2 Stream 1                */                   
	[74] = (uint32_t)DMA2_Stream2_IRQHandler,       /* DMA2 Stream 2                */                   
	[75] = (uint32_t)DMA2_Stream3_IRQHandler,       /* DMA2 Stream 3                */                   
	[76] = (uint32_t)DMA2_Stream4_IRQHandler,       /* DMA2 Stream 4                */                   
	[77] = (uint32_t)ETH_IRQHandler,                /* Ethernet                     */                   
	[78] = (uint32_t)ETH_WKUP_IRQHandler,           /* Ethernet Wakeup through EXTI line */                     
	[79] = (uint32_t)CAN2_TX_IRQHandler,            /* CAN2 TX                      */                          
	[80] = (uint32_t)CAN2_RX0_IRQHandler,           /* CAN2 RX0                     */                          
	[81] = (uint32_t)CAN2_RX1_IRQHandler,           /* CAN2 RX1                     */                          
	[82] = (uint32_t)CAN2_SCE_IRQHandler,           /* CAN2 SCE                     */                          
	[83] = (uint32_t)OTG_FS_IRQHandler,             /* USB OTG FS                   */                   
	[84] = (uint32_t)DMA2_Stream5_IRQHandler,       /* DMA2 Stream 5                */                   
	[85] = (uint32_t)DMA2_Stream6_IRQHandler,       /* DMA2 Stream 6                */                   
	[86] = (uint32_t)DMA2_Stream7_IRQHandler,       /* DMA2 Stream 7                */                   
	[87] = (uint32_t)USART6_IRQHandler,             /* USART6                       */                    
	[88] = (uint32_t)I2C3_EV_IRQHandler,            /* I2C3 event                   */                          
	[89] = (uint32_t)I2C3_ER_IRQHandler,            /* I2C3 error,                  */                          
	[90] = (uint32_t)OTG_HS_EP1_OUT_IRQHandler,     /* USB OTG HS End Point 1 Out   */                   
	[91] = (uint32_t)OTG_HS_EP1_IN_IRQHandler,      /* USB OTG HS End Point 1 In    */                   
	[92] = (uint32_t)OTG_HS_WKUP_IRQHandler,        /* USB OTG HS Wakeup through EXTI */                         
	[93] = (uint32_t)OTG_HS_IRQHandler,             /* USB OTG HS                   */                   
	[94] = (uint32_t)DCMI_IRQHandler,               /* DCMI                         */                   
	[95] = (uint32_t)0,                             /* CRYP crypto                  */                   
	[96] = (uint32_t)HASH_RNG_IRQHandler,           /* Hash and Rng                 */
	[97] = (uint32_t)FPU_IRQHandler,                /* FPU                          */
};

